package com.example.taobaounion.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseActivity;
import com.example.taobaounion.model.bean.TicketResult;
import com.example.taobaounion.presenter.ITicketPresenter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.PresenterManager;
import com.example.taobaounion.utils.ToastUtil;
import com.example.taobaounion.utils.UrlUtils;
import com.example.taobaounion.view.ITicketPagerCallback;

import butterknife.BindView;

public class TicketActivity extends BaseActivity implements ITicketPagerCallback {


    private ITicketPresenter mTicketPresenter;
    private boolean mHasTaobaoApp=false;
    @BindView(R.id.ticket_cover)
    public ImageView mCover;
    @BindView(R.id.ticket_code)
    public EditText mTicketCode;
    @BindView(R.id.ticket_copy_or_open_btn)
    public TextView mOpenOrCopyBtn;
    @BindView(R.id.ticket_cover_loading)
    public View loadingView;
    @BindView(R.id.ticket_load_retry)
    public View retryLoadText;
    @BindView(R.id.backPress)
    public ImageView backPress;
    @Override
    protected void initPresenter() {
        mTicketPresenter = PresenterManager.getInstance().getTicketPresenter();
        if (mTicketPresenter != null) {
            mTicketPresenter.registerViewCallback(this);
        }
        //判断是否安装淘宝
        //包名是com.taobao.taobao，检测是否安装这个应用要看是否获取得到这个包名
        //通过PowerShell的命令adb shell和logcat | grep taobao找到其包名和主页面Activity
        //I ActivityTaskManager: START u0 {act=android.intent.action.VIEW hwFlg=0x10 pkg=com.taobao.taobao
        // cmp=com.taobao.taobao/com.taobao.tao.TBMainActivity (has extras)} from uid 10147
        PackageManager pm = getPackageManager();
        //PackageManager.MATCH_UNINSTALLED_PACKAGES即使卸载只要保存有信息就获取信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo("com.taobao.taobao", PackageManager.MATCH_UNINSTALLED_PACKAGES);
            if(packageInfo!=null){//说明检测到手机有这个app
                mHasTaobaoApp=true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            mHasTaobaoApp=false;
        }
        LogUtils.d(this,"mHasTaobaoApp"+mHasTaobaoApp);
        //根据这个值去修改ui
        mOpenOrCopyBtn.setText(mHasTaobaoApp?"打开淘宝领劵":"复制淘口令");

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initEvent() {
        backPress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//finish()方法最终会调用onDestroy
            }
        });
        mOpenOrCopyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //复制淘口令
                //拿到内容
                String ticketCode = mTicketCode.getText().toString().trim();
                //获取ClipboardManager(剪切板)
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                //复制到粘贴板
                ClipData clipData = ClipData.newPlainText("ticket_code", ticketCode);
                cm.setPrimaryClip(clipData);
                //判断有没有淘宝(去加载数据访问网络的时候，淘宝那边那边后台能够检测到我们访问的接口，从而到了淘宝TBMainActivity主界面
                // 能够弹出对应的领劵窗口)
                if(mHasTaobaoApp){
                    //如果有就打开淘宝
                    Intent taobaoIntent=new Intent();
                    ComponentName componentName=new ComponentName("com.taobao.taobao","com.taobao.tao.TBMainActivity");
                    taobaoIntent.setComponent(componentName);
                    startActivity(taobaoIntent);
                }else {
                    //没有就提示复制成功
                    ToastUtil.showToast("已经复制，粘贴分享或打开淘宝");
                }

            }
        });
    }

    @Override
    protected void release() {
        if (mTicketPresenter != null) {
            mTicketPresenter.unregisterViewCallback(this);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ticket;
    }


    @Override
    public void onTicketLoaded(String cover, TicketResult result) {
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }
        if(mCover!=null&& !TextUtils.isEmpty(cover)){
            String coverPath = UrlUtils.getCoverPath(cover);
            Glide.with(this).load(coverPath).into(mCover);
        }
        if(result!=null&&result.getData().getTbk_tpwd_create_response()!=null){
            mTicketCode.setText(result.getData().getTbk_tpwd_create_response().getData().getModel());
        }

    }

    @Override
    public void onError() {
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }
        if(retryLoadText!=null){
            retryLoadText.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLoading() {
        if (loadingView != null) {
            loadingView.setVisibility(View.VISIBLE);
        }
        if(retryLoadText!=null){
            retryLoadText.setVisibility(View.GONE);
        }
    }

    @Override
    public void onEmpty() {

    }
}